/**
 *
 */
package com.gexiaobao.pigeon.app.util

import android.content.Context
import android.content.pm.PackageManager
import com.amap.api.maps.model.LatLng
import com.amap.api.services.core.LatLonPoint
import com.gexiaobao.pigeon.ui.dialog.AlertDialog


object AMapUtil {

    /**
     * 检测程序是否安装
     *
     * @param packageName
     * @return
     */
    fun isInstalled(packageName: String, context: Context): Boolean {
        val packageManager = context.packageManager
        val apps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
        for (app in apps) {
            if (packageName == app.packageName) {
                return true
            }
        }
        return false
    }

    /**
     * BD-09 坐标转换成 GCJ-02 坐标
     */
    fun BD2GCJ(bd: LatLng): LatLng? {
        val x = bd.longitude - 0.0065
        val y = bd.latitude - 0.006
        val z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI)
        val theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI)
        val lng = z * Math.cos(theta) //lng
        val lat = z * Math.sin(theta) //lat
        return LatLng(lat, lng)
    }

    fun IsEmptyOrNullString(s: String?): Boolean {
        return s == null || s.trim { it <= ' ' }.isEmpty()
    }

    /**
     * 把LatLonPoint对象转化为LatLon对象
     */
    fun convertToLatLng(latLonPoint: LatLonPoint): LatLng {
        return LatLng(latLonPoint.latitude, latLonPoint.longitude)
    }

    fun buildMsgSingle(
        context: Context,
        msg: String,
        negative: String,
        atInterNegative: () -> Unit,
        cancelAble: Boolean = true
    ) {
        AlertDialog(context).builder()
            .setMsg(msg)
            .setNegativeButton(negative) { atInterNegative() }
            .setCancelable(cancelAble).show()
    }
}